﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;

namespace SARControlLib
{
	/// <summary>
	/// TargetNote.xaml 的交互逻辑
	/// </summary>
	public partial class TargetNote : UserControl
	{
        private Brush bru = new SolidColorBrush(Color.FromArgb(0xFF,0xB3,0xB9,0xBE));
        private Dictionary<string, Border> allText = new Dictionary<string, Border>();        

        /// <summary>
        /// 添加一行新的文本
        /// </summary>
        /// <param name="name">文本的标识符，用于删除或者更新，不会显示</param>
        /// <param name="value">显示的文本</param>
        public void SARAddText(string name, string value)
        {
            Border newBorder = new Border();
            newBorder.BorderThickness = new Thickness(0, 1, 0, 1);
            newBorder.BorderBrush = bru;

            TextBlock newText = new TextBlock();
            newText.Text = value;
            newText.Foreground = bru;

            newBorder.Child = newText;

            textArea.Children.Add(newBorder);

            this.allText.Add(name, newBorder);

            this.Visibility = System.Windows.Visibility.Visible;
        }

        /// <summary>
        /// 删除一行文本
        /// </summary>
        /// <param name="name">要删除的这一行文本的标识符</param>
        public void SARRemoveText(string name)
        {
            Border border = this.allText[name];

            textArea.Children.Remove(border);

            this.allText.Remove(name);

            if (this.allText.Count == 0)
            {
                this.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        /// <summary>
        /// 更新一个文本
        /// </summary>
        /// <param name="name">要更新的这行文本的标识符</param>
        /// <param name="newValue">新的值</param>
        public void SARUpdateText(string name, string newValue)
        {
            Border border = this.allText[name];
            ((TextBlock)border.Child).Text = newValue;
        }

        public string[] SARGetAllNames()
        {
            IEnumerator e = allText.Keys.GetEnumerator();
            string[] result = new string[allText.Count];
            for (int i = 0; i < allText.Count; i++)
            {
                e.MoveNext();
                result[i] = (string)e.Current; ;
            }
            return result;
        }

        //0左上，1右上，2左下，3右下
        private int direation;
        public int SARDirection
        {
            get
            {
                return direation;
            }
            set
            {
                if (value < 0 || value > 3)
                {
                    return;
                }
                direation = value;
                if (value == 0)
                {
                    selector1.Visibility = System.Windows.Visibility.Visible;
                    selector2.Visibility = System.Windows.Visibility.Hidden;
                    selector3.Visibility = System.Windows.Visibility.Hidden;
                    selector4.Visibility = System.Windows.Visibility.Hidden;
                    textArea.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                    textArea.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    textArea.Margin = new Thickness(40, 40, 0, 0);
                }
                else if (value == 1)
                {
                    selector1.Visibility = System.Windows.Visibility.Hidden;
                    selector2.Visibility = System.Windows.Visibility.Visible;
                    selector3.Visibility = System.Windows.Visibility.Hidden;
                    selector4.Visibility = System.Windows.Visibility.Hidden;
                    textArea.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                    textArea.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                    textArea.Margin = new Thickness(0, 40, 40, 0);
                }else if (value == 2)
                {
                    selector1.Visibility = System.Windows.Visibility.Hidden;
                    selector2.Visibility = System.Windows.Visibility.Hidden;
                    selector3.Visibility = System.Windows.Visibility.Visible;
                    selector4.Visibility = System.Windows.Visibility.Hidden;
                    textArea.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
                    textArea.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    textArea.Margin = new Thickness(40, 4, 0, 40);
                }else
                {
                    selector1.Visibility = System.Windows.Visibility.Hidden;
                    selector2.Visibility = System.Windows.Visibility.Hidden;
                    selector3.Visibility = System.Windows.Visibility.Hidden;
                    selector4.Visibility = System.Windows.Visibility.Visible;
                    textArea.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
                    textArea.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                    textArea.Margin = new Thickness(0, 0, 40, 40);
                }
            }
        }

		public TargetNote()
		{
			this.InitializeComponent();
		}

	}
}